package com.woniuxy.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.woniuxy.pojo.Guest;
import com.woniuxy.pojo.OrderDetail;
import com.woniuxy.pojo.OrderDetail_dgy;
import com.woniuxy.pojo.Room;
import com.woniuxy.pojo.RoomType;

public interface OrderDetailMapper {
//	@Select("SELECT *FROM roomtype WHERE rtid IN ( SELECT rtid FROM room WHERE rid IN "
//			+ "( SELECT rid FROM orderdetail WHERE !((#{checkin} BETWEEN checkin AND checkout)"
//			+ " AND ( #{checkout}BETWEEN checkin AND checkout))))")
//	@Select("SELECT * FROM orderdetail od,room r,roomimg ro "
//			+ "WHERE od.rid=r.rid AND od.rid=ro.rid AND !((#{checkin} BETWEEN checkin AND checkout)"
//			+ "AND (#{checkout} BETWEEN checkin AND checkout))")
	@Select("SELECT *FROM room r,roomimg ro,roomtype rt WHERE r.rtid=rt.rtid AND r.rid=ro.rid"
			+ "  AND r.rid IN(SELECT DISTINCT(rid) FROM orderdetail WHERE !((#{checkin} BETWEEN checkin AND checkout)"
			+ " AND (#{checkout} BETWEEN checkin AND checkout)))")
	@Results({
		@Result(id=true,column="rid",property="rid"),
		@Result(column="rtid",property="roomType",one=@One(select="findroomType"))
	})
	public List<Room> dateByRoom(OrderDetail orderDetail);
	@Select("select *from roomtype where rtid=#{rtid}")
	public RoomType findroomType(int rtid);

	@Insert("insert into orderdetail(oldprice,newprice,checkin,checkout,rid,rtid,gid,oid) values(#{oldprice},#{newprice},#{checkin},#{checkout},#{rid},#{rtid},#{gid},#{oid})")
	int addOrderDetail(OrderDetail_dgy orderDetail);
	
	@Select("select * from orderdetail where oid = #{oid}")
	@Results({
		@Result(id=true,column="odid",property="odid"),
		@Result(column="gid",property="guest",
			one=@One(
				select="com.woniuxy.mapper.GuestMapper.findById"
					)),
		@Result(column="newprice",property="newprice")
	})
	OrderDetail_dgy findByOid(int oid);

	@Update("update orderdetail set scheckout=#{scheckout} where odid =#{odid}")
	int updateOrderDetail(OrderDetail_dgy orderDetail);

	@Select("select oid from orderdetail where gid=(select gid from guest where gname=#{gname} and gidcard=#{gidcard}) and scheckout is null")
	String findOidByGuest(Guest guest);

	@Update("update orderdetail set flag=1 where oid=(select oid from order where onumber=#{onumber})")
	int flagOrderDetail(String oNumber);
}
